package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcRepoSupplier;
import com.yxinsur.imc.business.domain.ImcSupplier;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.SupplierListForm;
import com.yxinsur.imc.business.mapper.ImcSupplierMapper;
import com.yxinsur.imc.business.service.IImcSupplierService;
import com.yxinsur.imc.business.vo.SupplierVo;
import com.yxinsur.imc.business.vo.select.SelectSupplier;
import com.yxinsur.imc.common.core.redis.RedisCache;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 保险公司/供应商Service业务层处理
 * 
 * @author imc
 * @date 2021-03-23
 */
@Service
public class ImcSupplierServiceImpl extends ServiceImpl<ImcSupplierMapper, ImcSupplier> implements IImcSupplierService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcSupplierServiceImpl.class);

    @Autowired
    private ImcSupplierMapper imcSupplierMapper;

    private static String SUPPLIER_SELECT_DATA = "supplier_select_data";

    /**
     * 查询保险公司/供应商下拉列表数据
     *
     * @return 保险公司/供应商
     */
    @Override
    public List<SelectSupplier> selectDataList() {
        List<ImcRepoSupplier> suppliers = this.imcSupplierMapper.selectMySuppliers(SecurityUtils.getTenantId());
        List<SelectSupplier> list = new ArrayList<>();
        for (ImcRepoSupplier supp:suppliers) {
            list.add(new SelectSupplier(supp.getId(),supp.getSupplierName()));
        }
        return list;
    }
    /**
     * 查询保险公司/供应商列表
     *
     * @param supplierForm 保险公司/供应商
     * @return 保险公司/供应商
     */
    @Override
    public List<SupplierVo> selectImcSupplierList(SupplierListForm supplierForm)
    {
        logger.info("供应商/保险公司库列表查询，条件：{}",supplierForm.toString());
        return imcSupplierMapper.selectSupplierList(supplierForm);
    }

    /**
     * 新增保险公司/供应商
     *
     * @param repoSupplierIds 保险公司/供应商
     * @return 结果
     */
    @Override
    public int insertImcSupplier(Long[] repoSupplierIds)
    {
        logger.info("我的供应商/保险公司新增操作：{}",repoSupplierIds);
        for (Long repoSupplierId:repoSupplierIds) {
            ImcSupplier supplier = new ImcSupplier();
            supplier.setRepoSupplierId(repoSupplierId);
            supplier.setCreateTime(DateUtils.getNowDate());
            supplier.setCreateId(SecurityUtils.getUsername());
            supplier.setIsDeleted(IsDeletedEnum.N.getCode());
            supplier.setTenantId(SecurityUtils.getTenantId());
            this.imcSupplierMapper.insert(supplier);
        }
        return repoSupplierIds.length;
    }
    /**
     * 批量删除保险公司/供应商
     *
     * @param ids 需要删除的保险公司/供应商ID
     * @return 结果
     */
    @Override
    public int deleteImcSupplierByIds(Long[] ids)
    {
        logger.info("我的供应商/保险公司删除操作：{}",ids);
        int count = 0;
        for(Long id:ids){
            ImcSupplier supplier = this.imcSupplierMapper.selectById(id);
            supplier.setIsDeleted(supplier.getId());
            supplier.setUpdateId(SecurityUtils.getUsername());
            supplier.setUpdateTime(DateUtils.getNowDate());
            count += this.imcSupplierMapper.updateById(supplier);
        }
        return count;
    }
}
